home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2770 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  56 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.Stanford.EDU!microunity!toms
  3. From: toms@MicroUnity.com (Tom Sanders)
  4. Subject: Re: help with pi algorithm
  5. Message-ID: <DLnHE8.7EK@microunity.com>
  6. Sender: usenet@microunity.com (news id)
  7. Organization: MicroUnity Systems Engineering, Inc.
  8. References:  <0fc_9601240111@csource.blaze.net.au>
  9. Date: Tue, 23 Jan 1996 20:10:55 GMT
  10.  
  11. In article <0fc_9601240111@csource.blaze.net.au>, Andrew.Nesbit@f396.n634.z3.fidonet.org (Andrew Nesbit) writes:
  12. |> Hi all!
  13. |> 
  14. |> I am a newbie C programmer trying to write an algorithm to work out the
  15. |> value of pi fairly accurately. I have written the following code, but I'm
  16. |> not sure whether it's considered 'nasty' programming, or whether it could
  17. |> be written to run more quickly.
  18. |> 
  19. |> #include <stdio.h>
  20. |> main()
  21. |> {
  22. |>    long double pi = 0;
  23. |>    long int = count;
  24. |>    for (count = 1; count <= 300000; count += 4) {
  25. |>       pi += 4.0 / count;            pi -= 4.0 / (count + 2);
  26. |>    }
  27. |>    printf("Value of pi is approx %.19Lf)", pi);
  28. |>    return 0;
  29. |> }
  30. |> 
  31. |> Any comments please???
  32. |> 
  33. |> Also, are there any 'standard' algorithm books which would demonstrate this
  34. |> sort of programming technique?
  35. |> 
  36. |> Thanks very much,
  37. |> Andrew Nesbit
  38. |> 
  39. |> --
  40. |> Andrew.Nesbit@f396.n634.z3.fidonet.org, Andrew Nesbit 3:634/396 (FidoNet)
  41. |> 
  42.  
  43. How accurate are you trying to compute pi?  Your math library should have
  44. pi defined to the number of bits available on your platform.  On my HPUX
  45. platform this line is from math.h:
  46. #  define M_PI          3.14159265358979323846
  47.  
  48. If you want more digits you will have to build an array to hold the
  49. additional digits.
  50.  
  51. What algorithm are you trying to implement here?  
  52. For a quick way I'v always liked 355/113 which gives pi to <0.00001%
  53. error.  This algorithm took 2.5 million times through the loop to beet this!
  54.  
  55. Tom Sanders
  56.